/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.pig.pigpen.view;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.apache.pig.data.DataBag;
import org.apache.pig.impl.logicalLayer.LogicalOperator;
import org.apache.pig.impl.logicalLayer.LogicalPlan;

//This is supposed to be the root class to hold the example data for tree viewer
public class ExampleData implements IDataContainer {

    Map<LogicalOperator, DataBag> derivedData;

    public ExampleData(Map<LogicalOperator, DataBag> derivedData) {
        this.derivedData = derivedData;
    }

    // public Object[] getChildren() {
    // //PigVariable[] pigVariable = new
    // PigVariable[derivedData.entrySet().size()];
    // PigVariable [] pigVariable = new PigVariable[getSize()];
    // int i = 0;
    // for(Map.Entry<LogicalOperator, DataBag> e : derivedData.entrySet()) {
    // LogicalOperator op = e.getKey();
    // if(op.getAlias() == null) continue;
    // pigVariable[i++] = new PigVariable(e.getKey(), e.getValue(), this);
    //            
    // }
    //        
    // return pigVariable;
    // }

    public Object[] getChildren() {
        LogicalPlan plan = derivedData.keySet().iterator().next().getPlan();
        List<LogicalOperator> result = new LinkedList<LogicalOperator>();

        // fetchChildren(plan, result, plan.getRoots());
        fetchChildren(plan, result, plan.getLeaves());
        PigVariable[] pigVariable = new PigVariable[result.size()];
        int i = 0;
        for (LogicalOperator op : result) {
            pigVariable[i++] = new PigVariable(op, derivedData.get(op), this);
        }
        return pigVariable;
    }

    // private void fetchChildren(LogicalPlan plan, List<LogicalOperator>
    // result, List<LogicalOperator> roots) {
    // if(roots == null)
    // return;
    // for(LogicalOperator op : roots) {
    // if(!result.contains(op) && op.getAlias() != null)
    // result.add(op);
    //            
    // fetchChildren(plan, result, plan.getSuccessors(op));
    // }
    //        
    //        
    // }

    private void fetchChildren(LogicalPlan plan, List<LogicalOperator> result,
            List<LogicalOperator> leaves) {
        if (leaves == null)
            return;
        for (LogicalOperator op : leaves) {
            if (!result.contains(op) && op.getAlias() != null)
                result.add(op);

            fetchChildren(plan, result, plan.getPredecessors(op));

        }
    }

    private int getSize() {
        int i = 0;
        for (LogicalOperator e : derivedData.keySet()) {
            if (e.getAlias() != null)
                i++;
        }
        return i;
    }

    public String getType() {
        // TODO Auto-generated method stub
        return "ExampleData";
    }

    public String getShortName() {
        return getType();
    }
}
